// pages/search/search.js
import request from '../../utils/request'
let isSend = false;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        placeholderContent: '',
        hotList: [],
        searchContent: '',
        searchList: [],
        historyList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getPlaceHolder();
        this.getSearchHistory();
    },

    async getPlaceHolder() {
        let result = await request("/search/default");
        let hotList = await request("/search/hot/detail");
        this.setData({
            placeholderContent: result.data.showKeyword,
            hotList: hotList.data
        });
    },

    //获取本地的历史记录
    getSearchHistory() {
        let historyList = wx.getStorageSync('searchHistory');
        if (historyList) {
            this.setData({
                historyList
            });
        }
    },

    handlerInputChange(event) {
        
        if (!event.detail.value.trim()) {
            this.setData({
                searchList: []
            });
            return;
        }

        this.setData({
            searchContent: event.detail.value.trim()
        });

        if (isSend) {
            return;
        }

        isSend = true;
        this.getSearchList();

        //函数节流

        setTimeout(async () => {
            isSend = false;
        }, 300);
    },

    async getSearchList() {
        let {
            searchContent,
            historyList
        } = this.data;
        let result = await request("/search", {
            keywords: searchContent,
            limit: 10
        });
        this.setData({
            searchList: result.result.songs
        });
        //将搜索的关键字添加到搜索历史记录中
        if (historyList.indexOf(searchContent) !== -1) {
            historyList.splice(historyList.indexOf(searchContent), 1);
        }
        historyList.unshift(searchContent);
        this.setData({
            historyList
        });
        wx.setStorageSync('searchHistory', historyList);

    },

    clear() {
        this.setData({
            searchContent: '',
            searchList: []
        });

    },

    deleteSearchHistory() {
        wx.showModal({
            content: '确认删除历史记录吗',
            success: (resp) => {
                if (resp.confirm) {
                    this.setData({
                        historyList: []
                    });
                    wx.removeStorageSync('searchHistory');
                }
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})